﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;

namespace McsUtility
{
    public static class NetHelper
    {
        public static int GetFreePort(int start = 40000)
        {
            int num = start;
            while (PortInUse(num))
            {
                num++;
            }
            return num;
        }

        internal static bool PortInUse(int port)
        {
            bool result = false;
            IPEndPoint[] activeTcpListeners = IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpListeners();
            for (int i = 0; i < activeTcpListeners.Length; i++)
            {
                if (activeTcpListeners[i].Port == port)
                {
                    result = true;
                    break;
                }
            }
            return result;
        }
    }
}
